package org.zachary.agent.interfaces;

import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zachary.agent.application.KafkaProducerService;
import org.zachary.agent.application.QQEmailService;
import org.zachary.agent.application.UserCreditService;
import org.zachary.agent.application.UserService;
import org.zachary.agent.domain.aggregate_model.user.User;
import org.zachary.agent.domain.aggregate_model.user_credit.UserCredit;
import org.zachary.agent.infrastructure.utils.Result;
import org.zachary.agent.interfaces.commands.CheckInfoCommand;
import org.zachary.agent.interfaces.commands.UpdateCreditCommand;
import org.zachary.commonmodule.entity.MqTransmissionJson;

import java.time.Instant;

@Api(tags = "用户信用分模块")
@RestController
@RequestMapping("/user/credit")
public class CreditController {
    private final UserCreditService userCreditService;
    private final KafkaProducerService kafkaProducerService;
    private final QQEmailService qqEmailService;
    private final UserService userService;

    public CreditController(UserCreditService userCreditService, KafkaProducerService kafkaProducerService, QQEmailService qqEmailService, UserService userService) {
        this.userCreditService = userCreditService;
        this.kafkaProducerService = kafkaProducerService;
        this.qqEmailService = qqEmailService;
        this.userService = userService;
    }
    @PostMapping("/admin/update")
    @PreAuthorize(value = "hasRole('admin')")
    public Result<Integer> updateCredit(@RequestBody CheckInfoCommand command) {
        System.out.println(command);
        User user=userService.findByUserId(command.userId());
        String message=command.message();
        String resultArray= command.stepResults();
        if(command.status()==0){
            return Result.fail("提交失败，状态被误设为 未完善");
        } else if (command.status()==1) {
            return Result.fail("提交失败，状态被误设为 审核中");
        } else if (command.status()==2) {
            userCreditService.notPass(command.checkerId(),command.userId());
            qqEmailService.sendAuditResultEmail(user.getEmail(),user.getNickName(),"信用分信息",false,resultArray,message);
            return Result.success("状态修改成功，用户未通过审核");
        } else if (command.status()==3) {
            userCreditService.pass(command.checkerId(),command.userId());
            qqEmailService.sendAuditResultEmail(user.getEmail(),user.getNickName(),"信用分信息",true,resultArray,message);

            return Result.success("状态修改成功，用户已通过审核");
        }else{
            return  Result.exception("不正确的状态码");
        }
    }
    @GetMapping("/get")
    public Result<UserCredit> getUserCreditById(@RequestParam("id") Integer id) {
        UserCredit uc=userCreditService.findById(id);
        if(uc==null){
            return Result.success("用户信息未完善，暂无信用分",uc);
        }
        System.out.println("获取用户信用分："+uc);
        return Result.success(uc);
    }

    @GetMapping("/score")
    public Result<Integer> getUserCreditScoreById(@RequestParam("id") Integer id) {
        UserCredit uc=userCreditService.findById(id);
        if(uc==null){
            return Result.success("用户信息未完善，暂无信用分",0);
        }
        System.out.println("获取用户信用分："+uc);
        return Result.success(uc.getCreditScore());
    }

    @PostMapping("/improveInfo")
    public Result<String> improveInfo(@RequestParam("id") Integer id, @RequestBody UpdateCreditCommand command){
        UserCredit uc=UserCredit.createUserCredit(id,command.getAccountType(), command.getIdNumber(), command.getAnnualIncome(),
                command.getQualification(),command.getJobType(), command.getMaritalStatus(),command.getIdCardFront(),command.getIdCardBack(),command.getWorkProof(),command.getEducationProof(),command.getIncomeProof());
        int i = userCreditService.updateInfoAndCalculateCreditScore(id, uc);
        userCreditService.setInImproving(id);
        return Result.success("用户信用分信息已完善，请等待审核");
    }

}
